<?php
###############   COPYLEFT GPLv3 LICENSE   ###############
##
## JFX Version 0.2.7
## Website Management Software
## www.jfxcms.com
##
## Copyright 2009 GPLv3 - http://www.opensource.org/licenses/gpl-3.0.html
##
## Anthony Gallon
## oi_antz@hotmail.com
##
## Permission is hereby granted to any person having a copy of this software
## to freely use and modify as required so long as the copyright notices
## and branding remain intact.
##
## Full license details available at http://www.jfxcms.com/license
##
###############   COPYLEFT GPLv3 LICENSE   ###############

/**
 * Facilitate the adding of radio buttons to a form
 * @author Anthony Gallon
 * @package Antz
 */


/**
 * Facilitate the adding of radio buttons to a form
 * @author Anthony Gallon
 * @package Antz
 */

class JFX_Form_Components_Radiogroup extends JFX_Form_Component
{
    /**
     * Options (radio objects) in a stack
     * @var array $opts
     */
    protected $opts = array();

    /**
     * Set our initial values
     * @param string $name
     * @param bool $selected
     */
    public function __construct($name, $label='', $selected='')
    {
        $this->name = $name;
        $this->label = $label;
        $this->addClass('radioGroup');
        $this->selected = $selected;
        $this->id = 'rg'.rand(0, 99999);
    }

    /**
     * Add a radio button to the Radio group
     * @param string $val
     * @param string $label
     * @param string $id
     * @return object JFX_Form_Components_Radio
     */
    public function add($val, $label='&nbsp;', $id='')
    {
        $key = count($this->opts);
        if($id=='') $id = $this->id.'_opt'.$key;
        $obj = new JFX_Form_Components_Radio;
        $obj->name = $this->name;
        $obj->setId($id);
        $obj->value = $val;
        $obj->label = $label;
        $this->opts[$key] = $obj;
        return $obj;
    }

    /**
     * Get our element as an HTML string
     * @return string $html
     */
    public function render()
    {
        $html = array();
        foreach($this->opts as $k=>$opt){
            $html[] = $opt->render($this->selected);
        };
        $returnString = '';
        if($this->showLabel) $returnString .= '<label for="'.$this->id.'">'.$this->label.'</label>';
        $returnString .= '<div class="radioGroup">'.implode("\n", $html).'</div>';
        if($this->wrap) $returnString = $this->wrap($returnString);
        return $returnString;
    }
}
